--lua script
--使用技能书学习技能

--#include "data\config\item\SkillBookItemData.lua" once
--#include "data\functions\Activity\Common\Consumes.lua" once

--[[
--添加buff的物品
--sysarg:实体的指针
--itemidx: 物品的ID
--itemPtr: 物品的指针
--ItemTable: 配置的参数列表 
--]]
function UseBookLearnSkill(sysarg,itemidx,itemPtr,ItemTable)
	if(Actor.getItemCount(sysarg,ItemTable.item_id) == 0) then
		Actor.sendTipmsg( sysarg,Lang.ScriptTips.x00013,ttFlyTip )
		return
	end
	
	local nJop = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
	if nJop == 0 or nJop == ItemTable.jop_id then
		local skillLevel = Actor.getSkillLevel(sysarg,ItemTable.skill_id)
		if skillLevel < ItemTable.skill_Level then
			if skillLevel ~= (ItemTable.skill_Level - 1) then
				Actor.sendTipmsg( sysarg,Lang.ScriptTips.x00201,ttFlyTip )
				return
			end

			local nResult = Actor.learnSkill(sysarg,ItemTable.skill_id, ItemTable.skill_Level)
			if nResult == 0 then
				local msg = string.format(Lang.ScriptTips.x00213, Actor.getName(sysarg), System.getSkillNameById(ItemTable.skill_id), ItemTable.skill_Level)	
				System.broadcastTipmsg(msg, ttScreenCenter + ttChatWindow)
				return true
			end
		else
			Actor.sendTipmsg( sysarg,Lang.ScriptTips.x00199,ttFlyTip )
			return
		end
	else
		Actor.sendTipmsg( sysarg,Lang.ScriptTips.x00198,ttFlyTip )
		return
	end
end


function UseBooAddExp(sysarg, itemidx, itemPtr, ItemTable, delType, itemUserNum, actorName, param)

	local skill_id = param or 1
	if not IsElemInList( ItemTable.skill_list, skill_id ) then
		return
	end

	local useSucc = Actor.setSkillProperty(sysarg, skill_id, 2, ItemTable.skill_exp)
	if useSucc then 
		Actor.removeItemByPtr(sysarg, itemPtr, 1, true, Lang.LogTips.UserItem007, GameLog.clUserItem)
		return true
	end
end

function AddItemBufferInit(sysarg)
  	for i = 1,table.getn(SkillBooKConfig) do
		local x = SkillBooKConfig[i]
		GlobalItemFn[x.item_id] = { func = UseBookLearnSkill, params = x }
  	end

	for i = 1,table.getn(SkillExpBookConfig) do
		local x = SkillExpBookConfig[i]
		GlobalItemFn[x.item_id] = { func = UseBooAddExp, params = x }
  	end
end

table.insert(InitFnTable,AddItemBufferInit)
